package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.DistrictBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TreeBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.mapper.TWaterCompanyMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 行政区域树
 * @author 许海宜
 *
 */
public class DistrictTree implements ServerInterface {
	private static Logger logger = LogManager.getLogger(DistrictTree.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		MessageBean<TreeBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", TreeBean.class);
		try {
			DistrictMapper mapper = factory.getMapper(DistrictMapper.class);

			DistrictBean selectBean = JsonUtils.objectToPojo(req.getData(), DistrictBean.class);;
			
			TWaterCompanyMapper twmapper = factory.getMapper(TWaterCompanyMapper.class,"_default");
			String adminAreaDivi = twmapper.selectCompanyByCode(userInfo.getCurWaterCode());
			selectBean.setStatus(1);//只查询启用的
//			selectBean.setIsLeaf(0);//只查询目录
			selectBean.setCode(adminAreaDivi==null?"all":adminAreaDivi);
			List<DistrictBean> list = mapper.findDistrict(selectBean);
			TreeBean treeBean = new TreeBean();
			
			if(list!=null && list.size()==1) {
				DistrictBean ebean = list.get(0);
				//创建根目录 
				treeBean.setId(ebean.getId());
				treeBean.setCode(ebean.getCode());
				treeBean.setName(ebean.getName());
				treeBean.setParent("");
				treeBean.setIsParent(false);
			}else {
				//创建根目录 
				treeBean.setId("0");
				treeBean.setCode(adminAreaDivi!=null?adminAreaDivi:selectBean.getCode());
				treeBean.setName("根目录");
				treeBean.setParent("");
				treeBean.setIsParent(true);
				for (DistrictBean bean : list) {
					TreeBean b = new TreeBean();
					b.setId(bean.getId()+"");
					b.setCode(bean.getCode());
					b.setName(bean.getName());
					b.setParent(treeBean.getId()+"");
					b.setIsParent(true);
					treeBean.getChildren().add(b);
				}
			}
			//递归下级目录
			//recTree(list,treeBean);

			info.setCode(Constant.MESSAGE_INT_SUCCESS);
			info.setDescription("success");
			info.setData(treeBean);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_DBFAIL);
			info.setDescription("数据库异常");
			logger.error("数据库异常",e);
			factory.close(false);
		}
		return info;
	}
	
	/**
	 * 递归查询下级目录树
	 * @param list
	 * @param treeBean
	 */
	private void recTree(List<DistrictBean> list,TreeBean treeBean) {
		for(DistrictBean bean:list) {
			//如果code是以父级开头，且长度多5位，说明这是他的下级
			if(bean.getCode().startsWith(treeBean.getCode()) && bean.getCode().length()==treeBean.getCode().length()+5) {
				TreeBean b = new TreeBean();
				b.setId(bean.getId()+"");
				b.setCode(bean.getCode());
				b.setName(bean.getName());
				b.setParent(treeBean.getId()+"");
				b.setIsParent(false);
				treeBean.setIsParent(true);
				treeBean.getChildren().add(b);
				recTree(list,b); //递归循环下级目录
			}
		}
	}
}
